import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import processConfig from "./process.env";
// import postcss from "./postcss.config.js";
import components from "unplugin-vue-components/vite";
import viteCompression from "vite-plugin-compression";
import autoImport from "unplugin-auto-import/vite";
import { NaiveUiResolver, VarletUIResolver } from "unplugin-vue-components/resolvers";
// import basicSsl from "@vitejs/plugin-basic-ssl";
// https://vitejs.dev/config/
const config = {
  base: "./",
  // publicPath: "/web/",
  server: {
    // host: "192.168.0.200",
    port: "5174",
    cors: true, // 允许跨域
    // https: false,
    proxy: {
      "/api": {
        target: "http://127.0.0.1:3000", // 代理的目标地址
        // target: "https://re.atispp.com/", // 代理的目标地址
        changeOrigin: true, // 开发模式，默认的origin是真实的
        rewrite: path => path.replace(/^\/api/, ""), // 路径重写，本项目不需要重写
      },
    },
  },
  plugins: [
    viteCompression({
      threshold: 1024, // 对大于 1k 的文件进行压缩
      // filter:"",//过滤器，对哪些类型的文件进行压缩，默认为 ‘/.(js|mjs|json|css|html)$/i’
      // verbose: true,//是否在控制台输出压缩结果，默认为 true
      // disable: false,//是否禁用压缩，默认为 false
      // deleteOriginFile:false,//压缩后是否删除原文件，默认为 false
      // algorithm:"gzip",//采用的压缩算法，默认是 gzip
      // ext:"",//生成的压缩包后缀
    }),
    vue(),
    // basicSsl(),
    components({
      resolvers: [NaiveUiResolver(), VarletUIResolver()],
    }),
    autoImport({
      resolvers: [],
      imports: [
        "vue",
        {
          "naive-ui": ["useDialog", "useMessage", "useNotification", "useLoadingBar"],
        },
      ],
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  css: {
    // postcss: {
    //   plugins: [postcss],
    // },
  },
};
// export default defineConfig(config);
export default defineConfig(({ command, mode, ssrBuild }) => {
  if (command === "serve") {
    return {
      // dev 独有配置
      ...config,
      define: {
        "process.env": processConfig.dev, //process.env配置
      },
    };
  } else {
    // command === 'build'
    return {
      // build 独有配置
      ...config,
      define: {
        "process.env": processConfig.production, //process.env配置
      },
    };
  }
});
